package org.lanqiao.practice_3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class TestDriver3 {
	
	/**
	 * @param c字符串中对应的字符
	 * @return
	 */
	public static int isChineseChar(char c) throws UnsupportedEncodingException{
		
		return String.valueOf(c).getBytes("GBK").length;
	}
	
	public static String cutString(String str,int count) throws UnsupportedEncodingException{
		StringBuffer buff=new StringBuffer();
		if(str!=null&&!str.equals("")){
			if(count>0&&count<str.getBytes("GBK").length){
				char ch;
				int i=0,j=0;
				do{
					ch=str.charAt(i++);
					buff.append(ch);
					ch=str.charAt(i);
					j+=isChineseChar(ch);					
				}while(j<count);
			}
		}
		String str1=buff.toString();
		return str1;
	}
	
	public static void main(String []args) throws UnsupportedEncodingException{
		System.out.print("请输入：");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, "GBK"));
		try {
			String str=reader.readLine();
			System.out.println(cutString(str,4));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
